package com.fengye.algorithms.leecode;

/**
 * @Author fengye
 * @Date 2023/4/8 10:12
 **/
public class StrStr28 {
    public static void main(String[] args) {
        String haystack = "sadbutsad";
        String needle = "but";
        System.out.println(new StrStr28().strStr(haystack,needle));
        System.out.println(new StrStr28().strStr2(haystack,needle));
    }

    public int strStr2(String haystack, String needle) {
        if(haystack.length() < needle.length()) {
            return -1;
        }
        if(haystack.equals(needle)) {
            return 0;
        }

        return haystack.indexOf(needle);
    }
    public int strStr(String haystack, String needle) {
        if(!haystack.contains(needle)) {
            return -1;
        }
        if(haystack.length() < needle.length()) {
            return -1;
        }
        if(haystack.equals(needle)) {
            return 0;
        }

        for(int i = 0; i <= haystack.length()-needle.length();i++) {
            String sub = haystack.substring(i,needle.length()+i);
            if(sub.equals(needle)) {
                return i;
            }
        }
        return -1;
    }
}
